using System;
using System.IO;
using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Environment;
using Sce.PlayStation.HighLevel.UI;
using VitaReader;

namespace VitaReaderLibPS
{
	internal class PSReader : VitaReader.IReader
	{
		private PSControl mControl;
		private PSView mView;
		private ReaderData mData;
		ReaderConfig	mConfig;
		private bool	mContinue;
		
		public PSReader()
		{
			mControl = new PSControl(this);
			mView = new PSView(this);
			
			String BookPath = "/Documents/Books";
			if( !Directory.Exists(BookPath) )
			{
				Directory.CreateDirectory(BookPath);
				foreach( String file in Directory.GetFiles("/Application/help", "*.txt" ) )
				{
					File.Copy(file, BookPath+"/"+Path.GetFileName(file) );
				}
			}
			
			mData = new ReaderData(this, BookPath, "/Documents/Booklist.txt" );
			
			//mData = new ReaderData(this, "/Application/txt", "/Documents/Booklist.txt" );
			mContinue = true;
			
			//init config
			mConfig = new ReaderConfig("/Documents/VitaReader.conf");
		}
		
		void IReader.Initialize()
		{
			//init data first
			IReaderData data = mData as IReaderData;
			data.Initialize();
			
			//then view
			IReaderView view = mView as IReaderView;
			view.Initialize();
			
			IReaderControl control = mControl as IReaderControl;
			control.Initialize();
		}
		
		void IReader.Shutdown()
		{
			mConfig.SaveConfig();
			
			IReaderView view = mView as IReaderView;
			view.Shutdown();
			
			IReaderControl control = mControl as IReaderControl;
			control.Shutdown();
			
			IReaderData data = mData as IReaderData;
			data.Shutdown();

		}
		
		void IReader.Update()
		{
			SystemEvents.CheckEvents ();
			
			IReaderView view = mView as IReaderView;
			view.Update();
			
			IReaderControl control = mControl as IReaderControl;
			control.Update();
		}
		
		String IReader.GetSystemLanguage()
		{
			String lang = SystemParameters.Language;
			if( lang == "zh-Hans" )
				lang = "zh-Hant";
			else if( lang != "ja-JP" )
				lang = "en-US";
			return lang;
		}
		
		IReaderView IReader.View			{get {return mView;}}
		
		IReaderControl IReader.Control		{get {return mControl;}}
		
		IReaderData IReader.Data			{get {return mData;}}
				
		bool IReader.Continue				{get {return mContinue;}}
		
		#region configs
		String	IReader.DownloadURL
		{
			get {return mConfig.ServerAddress;}
			set {mConfig.ServerAddress = value;}
		}
		String IReader.FontFile
		{
			get {return mConfig.FontFile;}
			set
			{
				if(mConfig.FontFile != value)
				{
					mConfig.FontFile = value;
					mView.UpdateFont(true);
				}
			}
		}
		
		int IReader.FontSize
		{
			get {return mConfig.FontSize;}
			set
			{
				if( mConfig.FontSize != value )
				{
					mConfig.FontSize = value;
					mView.UpdateFont(false);
				}
			}
		}
		
		UIColor IReader.TextColor
		{
			get {return mConfig.TextColor;}
			set {mConfig.TextColor = value;}
		}
		UIColor IReader.TextBackground
		{
			get {return mConfig.BackgroundColor;}
			set {mConfig.BackgroundColor = value;}
		}
		#endregion
		
		internal void Terminate()
		{
			mContinue = false;
		}
		
	}//class PSReader
	
	
	public class PSReaderFactory : IReaderFactory
	{
		IReader IReaderFactory.CreateReader(String Name)
		{
			IReader reader = null;
			Type type = Type.GetType(Name);
			if( type != null )
				reader = (IReader)Activator.CreateInstance(type);
			return reader;
		}
　　}
}
